package com.tangkun.environment.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tangkun.environment.network.NetworkUtil;

import retrofit.Retrofit;

/**
 * LazyLoadFragment[v 1.0.0]
 * classes:com.yixin.oaoinventory.base.LazyLoadFragment
 *
 * @author zhangdewu
 * @date 2016-07-21
 * @time 13:40
 * @description Fragment取消预加载点击才调接口数据
 */
public abstract class LazyLoadFragment extends BaseFragment {

    //-- Fragment当前状态是否可见
    protected boolean isVisible;
    protected Retrofit retrofit;
    protected NetworkUtil.FromNetwork fromNetwork;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        retrofit = NetworkUtil.getRetrofit();
        fromNetwork = retrofit.create(NetworkUtil.FromNetwork.class);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }


    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }

    /**
     * 不可见
     */
    protected void onInvisible() {
    }

    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();
}
